<?php
include 'sql.php';

$newSql = new SQLClass();
$avg_income = $newSql->get_avg_income();

// var_dump(count($avg_income));  0~count-1
$start_money = 1000000;
$avg_income[0]['money'] = $start_money;

// calculate rate
for ($i = 0; $i < count($avg_income) - 1; $i++) {
	// calculate close up down :tomorrow_close/today_close  
	$avg_income[$i]['rate'] = round($avg_income[$i + 1]['close'] / $avg_income[$i]['close'], 9);
}
// process end
$avg_income[count($avg_income) - 1]['rate'] = 1;
// calculate money
for ($i = 1; $i <= count($avg_income) - 1; $i++) {
	$avg_income[$i]['money'] = $avg_income[$i - 1]['money'] * $avg_income[$i - 1]['rate'];
}

/**
 * process the profits json data
 */
function processJson($avg_income)
{

	$res = [];
	$temp = [];

	// 进行赋值
	for ($m = 0; $m < count($avg_income); $m++) {
		$temp[0] = strtotime($avg_income[$m]['trade_date']) * 1000;
		$temp[1] = $avg_income[$m]['money'];
		array_push($res, $temp);
	}

	$res_json = json_encode($res);
	file_put_contents('json/1_year_stockCode_avg_income.json', $res_json);
}

/**
 * chang test.json
 */
$path = 'test.json';
$json_string = file_get_contents($path);
$data = json_decode($json_string, true);
function changeJson($data, $path)
{
	$res = [];
	$temp = [];
	// 收益数据  json
	//最开始设置初试资金
	$temp[0] = strtotime($data[0]['start_day']) * 1000;
	$temp[1] = 1000000;
	array_push($res, $temp);
	for ($m = 0; $m < count($data); $m++) {
		$temp[0] = strtotime($data[$m]['end_day']) * 1000;
		$temp[1] = round($res[$m][1] * (1 + $data[$m]['real_shouyilv'] / 100), 2);
		array_push($res, $temp);
	}
	$res_json = json_encode($res);
	file_put_contents('json/2_year_policy1_20.json', $res_json);
}


/**
 * run function
 * processJson: process avg_profit train_data to json
 * changeJson: change train_test date to json
 */

//processJson($avg_income);
 // changeJson($data, $path);

?>

<!DOCTYPE HTML>
<html>

<head>
	<meta charset="utf-8">
	<link rel="icon" href="https://jscdn.com.cn/highcharts/images/favicon.ico">
	<meta name="viewport" content="width=device-width, initial-scale=1">
	<style>
		/* css 代码  */
	</style>
	<script src="https://code.highcharts.com.cn/jquery/jquery-1.8.3.min.js"></script>
	<script src="https://code.highcharts.com.cn/highstock/highstock.js"></script>
	<script src="https://code.highcharts.com/modules/exporting.js"></script>
	<script src="https://code.highcharts.com.cn/highcharts-plugins/highcharts-zh_CN.js"></script>
</head>

<body>
	<!-- <div id="container_7_1_30" style="height: 400px; min-width: 600px"></div> -->
	<div id="container_7_2_30" style="height: 400px; min-width: 600px"></div>

	<!-- <script>
		// JS 代码   -- , '7_mouth_polocy1_30'  avg_income--
		var seriesOptions = [],
			seriesCounter = 0,
			names = ['7_mouth_avg_income', '7_mouth_polocy1_30'];
		/**
		 * Create the chart when all data is loaded
		 * this chart has a cache,change json should rename
		 * @returns {undefined}
		 */
		function createChart() {
			Highcharts.stockChart('container_7_1_30', {
				chart: {
					zoomType: null,
					// pinchType: null
				},
				rangeSelector: {
					selected: 5
				},
				yAxis: {
					labels: {
						formatter: function() {
							return (this.value > 0 ? ' + ' : '') + this.value + '%';
						}
					},
					plotLines: [{
						value: 0,
						width: 2,
						color: 'silver'
					}]
				},
				plotOptions: {
					series: {
						compare: 'percent',
						showInNavigator: true
					}
				},
				tooltip: {
					pointFormat: '<span style="color:{series.color}">{series.name}</span>: <b>{point.y}</b> ({point.change}%)<br/>',
					valueDecimals: 2,
					followTouchMove: false,
					split: true
				},
				series: seriesOptions
			});
		}
		$.each(names, function(i, name) {
			$.getJSON('json/' + name + '.json', function(data) {
				seriesOptions[i] = {
					name: name,
					data: data
				};
				// As we're loading the data asynchronously, we don't know what order it will arrive. So
				// we keep a counter and create the chart when all the data is loaded.
				seriesCounter += 1;
				if (seriesCounter === names.length) {
					createChart();
				}
			});
		});
	</script> -->


	<!-- compare one year train date three policies -->
	<!-- <script>
		// JS 代码   -- , '7_mouth_polocy1_30'  avg_income--
		var seriesOptions = [],
			seriesCounter = 0,
			names = ['1_year_stockCode_avg_income', '1_year_policy2_30'
			, '1_year_policy1_30', '1_year_stockCode'];
		/**
		 * Create the chart when all data is loaded
		 * this chart has a cache,change json should rename
		 * @returns {undefined}
		 */
		function createChart() {
			Highcharts.stockChart('container_7_2_30', {
				chart: {
					zoomType: null,
					// pinchType: null
				},
				rangeSelector: {
					selected: 5
				},
				yAxis: {
					labels: {
						formatter: function() {
							return (this.value > 0 ? ' + ' : '') + this.value + '%';
						}
					},
					plotLines: [{
						value: 0,
						width: 2,
						color: 'silver'
					}]
				},
				plotOptions: {
					series: {
						compare: 'percent',
						showInNavigator: true
					}
				},
				tooltip: {
					pointFormat: '<span style="color:{series.color}">{series.name}</span>: <b>{point.y}</b> ({point.change}%)<br/>',
					valueDecimals: 2,
					followTouchMove: false,
					split: true
				},
				series: seriesOptions
			});
		}
		$.each(names, function(i, name) {
			$.getJSON('json/' + name + '.json', function(data) {
				seriesOptions[i] = {
					name: name,
					data: data
				};
				// As we're loading the data asynchronously, we don't know what order it will arrive. So
				// we keep a counter and create the chart when all the data is loaded.
				seriesCounter += 1;
				if (seriesCounter === names.length) {
					createChart();
				}
			});
		});
	</script> -->

<script>
		// JS 代码   -- , '7_mouth_polocy1_30'  avg_income--
		var seriesOptions = [],
			seriesCounter = 0,
			names = ['1_year_stockCode_avg_income', '2_year_policy1_20'];
		/**
		 * Create the chart when all data is loaded
		 * this chart has a cache,change json should rename
		 * @returns {undefined}
		 */
		function createChart() {
			Highcharts.stockChart('container_7_2_30', {
				chart: {
					zoomType: null,
					// pinchType: null
				},
				rangeSelector: {
					selected: 5
				},
				yAxis: {
					labels: {
						formatter: function() {
							return (this.value > 0 ? ' + ' : '') + this.value + '%';
						}
					},
					plotLines: [{
						value: 0,
						width: 2,
						color: 'silver'
					}]
				},
				plotOptions: {
					series: {
						compare: 'percent',
						showInNavigator: true
					}
				},
				tooltip: {
					pointFormat: '<span style="color:{series.color}">{series.name}</span>: <b>{point.y}</b> ({point.change}%)<br/>',
					valueDecimals: 2,
					followTouchMove: false,
					split: true
				},
				series: seriesOptions
			});
		}
		$.each(names, function(i, name) {
			$.getJSON('json/' + name + '.json', function(data) {
				seriesOptions[i] = {
					name: name,
					data: data
				};
				// As we're loading the data asynchronously, we don't know what order it will arrive. So
				// we keep a counter and create the chart when all the data is loaded.
				seriesCounter += 1;
				if (seriesCounter === names.length) {
					createChart();
				}
			});
		});
	</script>
</body>

</html>